﻿using System.Collections;
using System.IO;
using UnityEngine;

public class UnpackStep: AssetsAction
{
    const string PackName = "abpack";

    bool unpacking;
    public override void Reset()
    {
#if UNITY_ANDROID
        if(PlayerPrefs.GetInt(PackName)==0)
        {
            //PlatformTool.Instance.StartCoroutine(GetDownloadSize(PackName));
            //request = PlayAssetDelivery.RetrieveAssetPackAsync(PackName);
            //if (request == null)
            //{
            //    _done = true;
            //}
            ///install-time 模式可以直接解压
            string pd = Application.persistentDataPath;
            string folder = pd + "/android";
            if (!Directory.Exists(folder))
                Directory.CreateDirectory(folder);
            PlatformTool.Instance.UnpakFileAsync(PackName, folder);
            unpacking = true;
        }
        else
        {
            _done = true;
        }
#else
        _done = true;
#endif
    }
    float time;
    public override void Update()
    {
        if(!_done)
        {
            time += Time.deltaTime;
            if(time>0.2f)
            {
#if UNITY_ANDROID
                if (unpacking)
                {
                    string info = PlatformTool.Instance.GetUnpackInfo();
                    if (info == null)
                    {
                        unpacking = false;
                        _done = true;
                        string pd = LocalSetting.RecordPath;
                        string[] all = Directory.GetFiles(LocalSetting.AssetsPath,"*.ini");
                        if(all!=null)
                        {
                            for (int i = 0; i < all.Length; i++)
                            {
                                string name = all[i].Replace(LocalSetting.AssetsPath,"");
                                string des = pd + name;
                                if (File.Exists(des))
                                    File.Delete(des);
                                File.Copy(all[i], des);
                            }
                        }
                        string path = LocalSetting.AssetsPath + "/version.json";
                        if(File.Exists(path))
                        {
                            AssetsVersion version = JsonUtility.FromJson<AssetsVersion>(File.ReadAllText(path));
                            LocalSetting.SetString("assets", "version", version.assversion);
                            LocalSetting.assetsVersion = version.assversion;
                        }
                        _done = true;
                        PlayerPrefs.SetInt(PackName, 1);
                    }
                    else
                    {
                        string[] ss = info.Split(',');
                        if (ss.Length > 1)
                        {
                            long.TryParse(ss[0], out _pos);
                            long.TryParse(ss[1], out _len);
                        }
                    }
                }
#endif
                time -= 0.2f;
            }
        }
    }

    public override string Tips
    { 
        get 
        {
#if UNITY_ANDROID
            if(unpacking)
            {
                if (_len == 0)
                    return "";
                double p = _pos;
                double all = _len;
                return "正在解压资源包:" + ((int)(p / all * 100)).ToString() + "%";
            }
#endif
            return ""; 
        } 
    }
    public override float Progress
    { 
        get 
        {
#if UNITY_ANDROID
            if (unpacking)
            {
                if (_len == 0)
                    return 0;
                double p = _pos;
                double all = _len;
                return (float)(p / all);
            }
#endif
            return 0; 
        } 
    }
}